<?php
/**
 * Access Control
 *
 * @author Mario Schillermann <info@rockit-flash.com>
 * @encoding UTF-8 äöüßÖÄÜ
 * @copyright © 2011 Mario Schillermann (info@rockit-flash.com)
 * @license GNU GPL v2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
 * @package Page
 * @subpackage Index
 * @link $HeadURL$
 * @version SVN: $Id$
 * @phpVersion >=5
 * @todo
 */
class Access_AppController extends Zend_Controller_Action
{

    public function init()
    {
        if($this->_request->isPost() && !$this->_request->getPost('roleListSelect')) {
            $this->_forward('save');
        }
    }
    /**
     * Show page content
     *
     * @return void
     */
    public function indexAction()
    {
        $this->setCss('app');

        $layout = Zend_Layout::getMvcInstance();
        $layout->appSubmit = array('<a href="javascript: submitform(\'ruleList\')">speichern</a>');
        $layout->appTab = Access_Model_Tab::render($this->view);

        $roleIdSelect = (int)$this->_request->roleIdSelect;

        $module = new Access_Model_Module;
        $moduleList = $module->getList();
        $access = new Access_Model_Access;

        $moduleList = $access->getList($roleIdSelect, $moduleList);
        $roleList = new Access_Model_Roles;

        $this->view->roleList = $roleList->get();
        $this->view->roleIdSelect = $roleIdSelect;
        $this->view->moduleList = $moduleList;
    }
    public function saveAction()
    {  
        try {
            $roleIdSelect = (int)$this->_request->roleIdSelect;
            $data = (array)$this->_request->data;

            $access = new Access_Model_Access;
            
            if($access->deleteRules($roleIdSelect) === FALSE) {

                $this->_forward(
                    'app',
                    'error',
                    'error',
                    array(
                        'message' => 'Alte Benutzerrechte konnten nicht entfernt werden!',
                        'back' => array('module' => 'rockitbos', 'app' => 'access')));
            } else {
            
                if($access->insertRules($roleIdSelect, $data)  === FALSE) {

                    $this->_forward(
                        'app',
                        'error',
                        'error',
                        array(
                            'message' => 'Neue Benutzerrechte konnten nicht erstellt werden!',
                            'back' => array('module' => 'rockitbos', 'app' => 'access')));
                }
            }
        } catch (Exception $e) {

            $this->_forward('app', 'error', 'error', array('log' => $e));
        }
    }

    protected function setCss($file) {

        $this->view
             ->headLink()
             ->appendStylesheet(
                 DIRECTORY_SEPARATOR . 'access' .
                 DIRECTORY_SEPARATOR . 'css' .
                 DIRECTORY_SEPARATOR . $file . ".css"
               );
    }
}